package com.tianyu.inspection.common;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2020.12.17 - 15:48
 **********************************/

public class InspectLineExecutor extends ThreadPoolExecutor {

	private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);

	public InspectLineExecutor() {
		super(4, 20, 10, TimeUnit.SECONDS,
			new LinkedBlockingQueue<>(), new ThreadFactory() {
				private final AtomicInteger threadNumber = new AtomicInteger(1);
				private final int currentPoolNumber = POOL_NUMBER.getAndIncrement();
				@Override
				public Thread newThread(Runnable r) {
					SecurityManager s = System.getSecurityManager();
					ThreadGroup group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
					String namePrefix = "常规任务池-" + currentPoolNumber + "-线程-";
					Thread t = new Thread(group, r,
							namePrefix + threadNumber.getAndIncrement(),
							0);
					if (t.isDaemon()) {
						t.setDaemon(false);
					}
					if (t.getPriority() != Thread.NORM_PRIORITY) {
						t.setPriority(Thread.NORM_PRIORITY);
					}
					return t;
				}
			}, new ThreadPoolExecutor.CallerRunsPolicy()
		);
	}
}
